package com.fz.crowdfunding.Controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fz.crowdfunding.bean.Permission;
import com.fz.crowdfunding.bean.User;
import com.fz.crowdfunding.manager.service.UserService;
import com.fz.crowdfunding.util.AjaxResult;
import com.fz.crowdfunding.util.Const;
import com.fz.crowdfunding.util.MD5Util;

@Controller
public class DispatcherController {
	
	@Autowired
	private UserService userService;
	/**
	 * 跳转首页
	 * @param session
	 * @return
	 */
	@RequestMapping("/index")
	public String index(){
		return "index";
	}
	/**
	 * 登录
	 * @param session
	 * @return
	 */
	@RequestMapping("/login")
	public String login(){
		return "login";
	}
	/**
	 * 跳转后台主页面
	 * @param session
	 * @return
	 */
	@RequestMapping("/main")
	public String main(HttpSession session){
		return "main";
	}
	/**
	 * 注销功能
	 * @param session
	 * @return
	 */
	@RequestMapping("/logout")
	public String logout(HttpSession session){
		
		session.invalidate();//销毁session对象
		
		return "redirect:/index.htm";
	}
	//异步请求，返回json
	//@ResponseBody注解，结合jackson组件，将返回结果转换为字符串，将Json字符串已流的形式返回给客户端
	@ResponseBody
	@RequestMapping("/doLogin")
	public Object doLogin(String loginacct,String userpswd,String type,HttpSession session){
		AjaxResult result = new AjaxResult();
		try {
			Map<String,Object> paramMap = new HashMap<String,Object>();
			paramMap.put("loginacct", loginacct);
			paramMap.put("userpswd", MD5Util.digest(userpswd));
			paramMap.put("type", type);
			User user = userService.queryUserlogin(paramMap);
			//定义常量类，存放key，前台页面就可以根据key取到属性值
			session.setAttribute(Const.LOGIN_USER, user);
			
			//加载当前登录用户的许可权限
			//User user = (User)session.getAttribute(Const.LOGIN_USER);
			List<Permission> myPermissions = userService.queryPermissionByUserId(user.getId());
			
			Permission permissionRoot = null;
			
			Set<String> myUris = new HashSet<String>();
			Map<Integer, Permission> map = new HashMap<Integer,Permission>();
			//将所有菜单数据放入Map集合
			for (Permission p : myPermissions) {
				map.put(p.getId(), p);
				//将当前登录用户的可以访问的许可路径放入set集合，并返回session域中
				myUris.add("/"+p.getUrl());
			}
			session.setAttribute(Const.MY_URIS, myUris);
			for (Permission permission : myPermissions) {
				//通过子菜单查找父菜单
				//子菜单
				Permission child = permission; //假设为子菜单
				//如果该对象的pid是0,则表示其为根菜单
				if(child.getPid() == 0){
					permissionRoot = permission;
				}else{
					//根据子菜单的pid得到对应的父菜单对象
					Permission parent = map.get(child.getPid());
					parent.getChildren().add(child);
				}
			}
			session.setAttribute("permissionRoot", permissionRoot);
			
			result.setSuccess(true);
		} catch (Exception e) {
			result.setMessage("登录失败");
			e.printStackTrace();
			result.setSuccess(false);
		}
		return result;
	}
	
/*	//接收前台传过来的参数时，方法参数名和表单中的name名一致就可以直接接收
	@RequestMapping("/doLogin")
	public String doLogin(String loginacct,String userpswd,String type,HttpSession session){
		Map<String,Object> paramMap = new HashMap<String,Object>();
		paramMap.put("loginacct", loginacct);
		paramMap.put("userpswd", userpswd);
		paramMap.put("type", type);
		User user = userService.queryUserlogin(paramMap);
		//定义常量类，存放key
		session.setAttribute(Const.LOGIN_USER, user);
		//重定向到main.jsp页面,防止重复提交表单
		return "redirect:/main.htm";
	}*/
	
	
}
